/*
 * Copyright 2005-2010 the original author or authors.
 * 
 *      http://www.derbysoft.com/
 *
 * Project demo
 */
package com.zhisheng.core.service.impl;

import java.io.Serializable;
import java.util.List;

import com.zhisheng.core.dao.GenericDAO;
import com.zhisheng.core.page.Page;
import com.zhisheng.core.service.GenericService;

@SuppressWarnings("unchecked")
public abstract class BaseServiceImpl<T, ID extends Serializable, M extends GenericDAO<T, ID>> implements GenericService<T, ID>{

	protected M entityDao;
	
	public <E> List<E> findAll(Class<E> c) {
		return entityDao.findAll(c);
	}
	
	
	public Page findByPage(Page page, Class c){
		return entityDao.findByPage(page, c);
	}

	public T findById(ID id) {
		return (T) entityDao.findById(id);
	}
	
	public T loadById(ID id) {
		return (T) entityDao.loadById(id);
	}

    public void save(Object entity) {
    	entityDao.save(entity);
    }


	public void update(Object entity) {
		entityDao.update(entity);
    }
	
	public void saveOrUpdate(Object entity) {
		entityDao.saveOrUpdate(entity);
	}
    
    public void delete(T entity) {
    	entityDao.delete(entity);
    }
	
	public void batchDeleteEntities(String [] ids) {
		if (ids != null) {
			for (String id : ids) {
				entityDao.delete(id);
			}
		}
	}

	/**
	 * @param c
	 * @param roleIds
	 */
	public void batchDeleteEntities(Class c, String[] ids) {
		if (ids != null) {
			for (String id : ids) {
				entityDao.delete(c, id);
			}
		}
	}

	/**
	 * @param class1
	 * @param string
	 * @return
	 */
	public <S> S findById(Class<S> c, Serializable string) {
		return (S) entityDao.findEntityById(string, c);
	}
	
}
